﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.users
{
    public partial class cur_consume : Web.UI.ManagePage
    {
        Model.manager mgr;
        string appWhere;
        private int id = 0;
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        protected int totalCount;
        protected int page;
        protected int pageSize;
        protected string startTime = string.Empty;
        protected string endTime = string.Empty;
        protected string keywords = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            //&&客户主键id
            this.id = DTRequest.GetQueryInt("id");
            string _action = DTRequest.GetQueryString("action");
            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.View.ToString())
            {
                this.action = DTEnums.ActionEnum.View.ToString();
                if (!new BLL.sf_user_info().Exists(this.id))
                {
                    JscriptMsg("客户编号不存在或已被删除！", "back");
                    return;
                }
            }
            this.keywords = DTRequest.GetQueryString("keywords");
            this.startTime = DTRequest.GetQueryString("start_time");
            this.endTime = DTRequest.GetQueryString("end_time");
            this.pageSize = GetPageSize(10); //每页数量
            if (!Page.IsPostBack)
            {
                if (Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager == null)
                {
                    Response.Redirect("/admin/login.aspx");
                }
                mgr = Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager;
                //&&加盟商
                RptBind(CombSqlTxt(this.startTime, this.endTime, keywords), " addTime desc,id desc");
            }
        }

        #region 数据绑定
        private void RptBind(string _strWhere, string _orderby)
        {
            BLL.sf_user_info userBll = new BLL.sf_user_info();
            Model.sf_user_info model = userBll.GetModel(this.id);
            appWhere = Utils.getWhereByInter(model.busnieseNum, model.appNum);
            if (model != null)
            {
                lblCurCustomer.Text = model.nickName;
                this.page = DTRequest.GetQueryInt("page", 1);
                txtKeyWords.Text = this.keywords;
                txtStartTime.Text = this.startTime;
                txtEndTime.Text = this.endTime;
                //&&支付完成的才算消费
                _strWhere = " isPay=2 and userNum='" + model.userNum + "' and " + appWhere + _strWhere;
                BLL.sf_goods_order bll = new BLL.sf_goods_order();
                this.rptList.DataSource = bll.GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
                this.rptList.DataBind();

                //绑定页码
                txtPageNum.Text = this.pageSize.ToString();
                string pageUrl = Utils.CombUrlTxt("cur_consume.aspx", "start_time={0}&end_time={1}&keywords={2}&page={3}&id={4}", this.startTime, this.endTime, this.keywords, "__id__", this.id.ToString());
                PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
            }
            else
            {
                this.rptList.DataSource = null;
                this.rptList.DataBind();
            }
        }
        #endregion

        #region 组合SQL查询语句
        protected string CombSqlTxt(string _startTime, string _endTime, string _keywords)
        {
            System.Text.StringBuilder strTemp = new System.Text.StringBuilder();
            _startTime = _startTime.Replace("'", "");
            _endTime = _endTime.Replace("'", "");
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(startTime))
            {
                strTemp.Append(" and addTime between '").Append(startTime).Append("'");
                if (!string.IsNullOrEmpty(endTime))
                    strTemp.Append(" and '").Append(endTime).Append("'");
                else
                    strTemp.Append(" and '").Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")).Append("'");
            }
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (orderNum like  '%")
                      .Append(_keywords)
                      .Append("%' or userNum like '%")
                      .Append(_keywords)
                      .Append("') ");
            }
            return strTemp.ToString();
        }
        #endregion

        #region 返回每页数量
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("cur_consume_page_size"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("cur_consume.aspx", "start_time={0}&end_time={1}&keywords={2}&id={3}", Request.Form["txtStartTime"], Request.Form["txtEndTime"], txtKeyWords.Text, this.id.ToString()));
        }

        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("cur_consume_page_size", _pagesize.ToString(), 14400);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("cur_consume.aspx", "start_time={0}&end_time={1}&keywords={2}&id={3}", this.startTime, this.endTime, this.keywords, this.id.ToString()));
        }

    }
}